<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="运单号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入运单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运单状态" prop="status">
        <MultipleSelect :selectOptions="dict.type.OrderStatus" :mulSelectLoading="false" :mulSelecteds="queryParams.statusArray"
          @update:updateMultipleSelect="val => queryParams.statusArray = val">
        </MultipleSelect>
      </el-form-item>
      <el-form-item label="实际到达时间">
        <el-date-picker
          v-model="daterangeFactArriveTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="实际发运时间">
        <el-date-picker
          v-model="daterangeFactLeaverTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="是否有异常" prop="isAbnormal">
        <el-select v-model="queryParams.isAbnormal" placeholder="是否有异常" clearable>
          <el-option
            v-for="dict in dict.type.CommIsNot"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="license">
        <el-input
          v-model="queryParams.license"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运作模式" prop="operationPattern">
        <el-select v-model="queryParams.operationPattern" placeholder="请选择运作模式" clearable>
          <el-option
            v-for="dict in dict.type.OperationPattern"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="司机名称" prop="outDriver">
        <el-input
          v-model="queryParams.outDriver"
          placeholder="请输入司机名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起运站" prop="jcFromOrganizationId">
        <el-select clearable filterable v-model="queryParams.jcFromOrganizationId" placeholder="请选择起运站" @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in dict.type.DB_JC_ORG"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="目的站" prop="jcToOrganizationId">
        <el-select clearable filterable v-model="queryParams.jcToOrganizationId" placeholder="请选择目的站" @keyup.enter.native="handleQuery">
          <el-option
            v-for="dict in dict.type.DB_JC_ORG"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['shipment:shipment:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['shipment:shipment:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['shipment:shipment:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['shipment:shipment:export']">导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-printer" size="mini" :disabled="multiple" @click="handlePrint"
                   v-hasPermi="['shipment:shipment:print']">打印运单
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-printer" size="mini" :disabled="multiple" @click="handleSignDownload"
                   v-hasPermi="['shipment:shipment:print']">下载签收单
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-printer" size="mini" :disabled="multiple" @click="handleSignPrint"
                   v-hasPermi="['shipment:shipment:print']">打印签收单
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" plain icon="el-icon-s-check" size="mini" :disabled="multiple" @click="handleAbnormalEdit"
                   v-hasPermi="['shipment:shipment:edit']">异常修改
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button type="success" plain icon="el-icon-s-promotion" size="mini" @click="handleDispatch"-->
<!--                   v-hasPermi="['shipment:shipment:export']">发运-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button type="danger" plain icon="el-icon-circle-check" size="mini" @click="handleArrive"-->
<!--                   v-hasPermi="['shipment:shipment:export']">运抵-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shipmentList"
              @selection-change="handleSelectionChange"
              @row-dblclick="handleUpdate"
              :default-sort = "{prop: 'time', order: 'ascending'}"
              @sort-change="sortChange" >
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="组织机构" align="center" prop="dtMap.jcOrganizationIdDtLabel" fixed/>
      <el-table-column label="运单号" align="center" prop="code" width="130" fixed/>
      <el-table-column label="运单日期" align="center" prop="time" width="180" sortable="custom" :sort-orders="['descending','ascending']"/>
      <el-table-column label="运单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="dict.type.OrderStatus" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="收入成本确认" align="center" prop="isConfirm">
        <template #default="scope">
          <dict-tag :options="dict.type.ConfirmStatus" :value="scope.row.isConfirm"/>
        </template>
      </el-table-column>
      <el-table-column label="车牌号" align="center" prop="license" width="100"/>
      <el-table-column label="起运站" align="center" prop="dtMap.jcFromOrganizationIdDtLabel"/>
      <el-table-column label="当前站" align="center" prop="dtMap.jcNowOrganizationIdDtLabel"/>
      <el-table-column label="下一站" align="center" prop="dtMap.jcNextOrganizationIdDtLabel"/>
      <el-table-column label="目的站" align="center" prop="jcToOrganizationId" width="100" sortable="custom" :sort-orders="['descending','ascending']">
        <template #default="scope">
          <span>{{ scope.row.dtMap.jcToOrganizationIdDtLabel }}</span>
        </template>
      </el-table-column>
      <el-table-column label="途径站" align="center" prop="orgnameList"/>
      <el-table-column label="总金额" align="center" prop="totalFee"/>
      <el-table-column label="订单号" align="center" prop="orderCode" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="客户单号" align="center" prop="relateBill1" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="运作模式" align="center" prop="operationPattern">
        <template #default="scope">
          <dict-tag :options="dict.type.OperationPattern" :value="scope.row.operationPattern"/>
        </template>
      </el-table-column>
      <el-table-column label="承运商类型" align="center" prop="carrierType">
        <template #default="scope">
          <dict-tag :options="dict.type.Cytp" :value="scope.row.carrierType"/>
        </template>
      </el-table-column>
      <el-table-column label="承运商" align="center" prop="dtMap.jcCarrierIdDtLabel"/>
      <el-table-column label="承运商是否超期" align="center" prop="carriageIsExceed">
        <template #default="scope">
          <dict-tag :options="dict.type.CommIsNot" :value="scope.row.carriageIsExceed"/>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="number"/>
      <el-table-column label="货值" align="center" prop="value"/>
      <el-table-column label="体积" align="center" prop="volume"/>
      <el-table-column label="重量" align="center" prop="weight"/>
      <el-table-column label="实际发运时间" align="center" prop="factLeaverTime" width="180"/>
      <el-table-column label="实际到达时间" align="center" prop="factArriveTime" width="180"/>
      <el-table-column label="创建人" align="center" prop="createName"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="100"/>
      <el-table-column label="备注" align="center" prop="description"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <shipment-form ref="shipmentForm" @change="getList"/>
    <shipment-abnormal-form ref="shipmentAbnormalForm" @change="getList"/>
    <print-sign ref="printSign"/>
  </div>
</template>

<script>
import {listShipment, delShipment, dispatch, arrival, printShipmentData} from "@/api/shipment/shipment";
import ShipmentForm from './form.vue'
import ShipmentAbnormalForm from './formAbnormal.vue'
import MultipleSelect from "@/components/MultipleSelect/index.vue";
import PrintSign from "./printSign.vue";

export default {
  name: "NewShipment",
  dicts: ['CommDataStatus', 'Cytp', 'DB_JC_ORG', 'CommIsNot', 'OrderStatus', 'ConfirmStatus', 'CarrierType', 'OperationPattern', 'TransportMenth', 'Clearing', 'CollectMethod', 'TaxRate'],
  components: {PrintSign, MultipleSelect, ShipmentForm, ShipmentAbnormalForm},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      mulSelecteds: [],
      // 总条数
      total: 0,
      // 新运单管理表格数据
      shipmentList: [],
      // 弹出层标题
      title: "",
      // 是否收入成本确认时间范围
      daterangeCreateTime: [],
      // 是否收入成本确认时间范围
      daterangeFactArriveTime: [],
      // 是否收入成本确认时间范围
      daterangeFactLeaverTime: [],
      // 是否收入成本确认时间范围
      daterangeTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        status: null,
        statusArray: [],
        statusStr: null,
        backNumber: null,
        carriageIsExceed: null,
        carrierType: null,
        factArriveTime: null,
        factLeaverTime: null,
        feeType: null,
        isAbnormal: null,
        license: null,
        number: null,
        operationPattern: null,
        orderCode: null,
        outDriver: null,
        planLeaveTime: null,
        refuse: null,
        relateBill1: null,
        requireArriveTime: null,
        shipmentMethod: null,
        tan: null,
        time: null,
        value: null,
        volume: null,
        weight: null,
        jcCarrierId: null,
        jcDriverId: null,
        jcOrganizationId: null,
        jcOrganizationIdDtLabel: null,
        jcSettlementId: null,
        jcVehicleId: null,
        jcVehicleHeadId: null,
        actionTime: null,
        jcFromOrganizationId: null,
        jcFromOrganizationIdDtLabel: null,
        jcNowOrganizationId: null,
        jcNowOrganizationIdDtLabel: null,
        jcNextOrganizationId: null,
        jcNextOrganizationIdDtLabel: null,
        jcToOrganizationId: null,
        jcToOrganizationIdDtLabel: null,
        orgIds: null,
        outPhone: null,
        orgnameList: null,
        jzWeight: null,
        isConfirm: null
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询新运单管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeFactArriveTime && '' != this.daterangeFactArriveTime) {
        this.queryParams.params["beginFactArriveTime"] = this.daterangeFactArriveTime[0];
        this.queryParams.params["endFactArriveTime"] = this.daterangeFactArriveTime[1];
      }
      if (null != this.daterangeFactLeaverTime && '' != this.daterangeFactLeaverTime) {
        this.queryParams.params["beginFactLeaverTime"] = this.daterangeFactLeaverTime[0];
        this.queryParams.params["endFactLeaverTime"] = this.daterangeFactLeaverTime[1];
      }
      if (null != this.daterangeTime && '' != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0];
        this.queryParams.params["endTime"] = this.daterangeTime[1];
      }
      if (this.queryParams.statusArray) {
        this.queryParams.statusStr = this.queryParams.statusArray.join(",");
      }
      listShipment(this.queryParams).then(response => {
        this.shipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeFactArriveTime = [];
      this.daterangeFactLeaverTime = [];
      this.daterangeTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    sortChange(row) {
      const { prop, order } = row
      this.queryParams.orderByColumn = prop
      this.queryParams.isAsc = order === 'descending' ? 'desc' : 'asc'
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.shipmentForm.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      this.$refs.shipmentForm.handleUpdate(id);
    },
    handleAbnormalEdit() {
      const id = this.ids
      this.$refs.shipmentAbnormalForm.handleUpdate(id);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除新运单管理运单号为"' + ids + '"的数据项？').then(function () {
        return delShipment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shipment/shipment/export', {
        ...this.queryParams
      }, `shipment_${new Date().getTime()}.xlsx`)
    },
    /** 打印运单按钮操作 */
    handlePrint() {
      const ids = this.ids
      this.$confirm('是否确认打印选择的运单?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.download('/shipment/shipment/printShipment/' + ids, {}, `shipment_${new Date().getTime()}.pdf`)
      }).catch(() => {
      });
      // 跳转打印页面 shipmentPrint.vue
      // const ids = this.ids
      // this.$tab.openPage("打印运单", "/product/shipment-manger/print/index/" + ids);
    },
    handleSignDownload() {
      const ids = this.ids
      this.$confirm('是否确认下载选择的签收单?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.download('/shipment/shipment/printSignPdf/' + ids, {}, `shipment_${new Date().getTime()}.pdf`)
      }).catch(() => {
      });
    },
    // handleSignPrint() {
    //   const ids = this.ids
    //   this.$refs.printSign.openDialog(ids)
    // },
    handleDispatch(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认发运？').then(function () {
        return dispatch(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("发运成功");
      }).catch(() => {
      });
    },
    // 运抵
    handleArrive(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认运抵？').then(function () {
        return arrival(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("运抵成功");
      }).catch(() => {
      });
    }
  }
};
</script>
